package com.huike.web.controller.system;

import com.huike.clues.domain.result.UserInfoAjaxResult;
import com.huike.clues.service.ISysDictService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDictType;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.core.redis.RedisCache;
import com.huike.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "数据字典类型信息相关接口")
@Slf4j
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {
    @Resource
    ISysDictService iSysDictService;
    @Resource
    private RedisCache redisCache;
    @PostMapping()
    @ApiOperation("新增字典类型")
    public UserInfoAjaxResult insertDict(@RequestBody SysDictType sysDictType){
        log.info("新增字典类型，操作人:{}", SecurityUtils.getUsername());
        iSysDictService.insertDict(sysDictType);
        return UserInfoAjaxResult.success();
    }
    @PutMapping()
    @ApiOperation("修改字典类型")
    public UserInfoAjaxResult updateDict(@RequestBody SysDictType sysDictType){
        log.info("修改字典类型，操作人:{},修改对象名:{}", SecurityUtils.getUsername(),sysDictType.getDictType());
        iSysDictService.updateDict(sysDictType);
        return UserInfoAjaxResult.success();
    }
    @DeleteMapping("/clearCache")
    @ApiOperation("清空缓存")
    public UserInfoAjaxResult clearCache(){
        log.info("清空缓存，操作人:{}", SecurityUtils.getUsername());
        iSysDictService.clearCache();
        return UserInfoAjaxResult.success();
    }
    @GetMapping("/export")
    @ApiOperation("导出数据")
    public AjaxResult export(){
        log.info("导出数据，操作人:{}", SecurityUtils.getUsername());
        return iSysDictService.export();
    }
    @ApiOperation("分页查询数据")
    @GetMapping("/list")
    public TableDataInfo page(SysDictType sysDictType) {
            log.info("分页查询数据");
            startPage();
            List<SysDictType> list = iSysDictService.pageList(sysDictType);
            TableDataInfo dataTable = getDataTable(list);
            return dataTable;
        }
    @GetMapping("/{dictId}")
    public AjaxResult<SysDictType> findBy(@PathVariable Long dictId) {
        log.info("回显数据:{}", dictId);
        return AjaxResult.success(iSysDictService.findByDictId(dictId));
    }
    @ApiOperation("获取字典选择框列表")
    @GetMapping("/optionselect")
    public AjaxResult<SysDictType> optionSelect() {
        log.info("获取字典选择框列表");
        SysDictType sysDictType = iSysDictService.optionSelect();
        return AjaxResult.success(sysDictType);
    }
    @ApiOperation("根据id批量删除字典")
    @DeleteMapping("/{dictIds}")
    public AjaxResult dictIds(List<Long> dictIds) {
        log.info("根据id批量删除字典", dictIds);
        iSysDictService.delete(dictIds);
        return AjaxResult.success();
    }
}
